package com.offcn.web;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.offcn.entity.Course_User;
import com.offcn.service.Course_UserService;
import com.offcn.service.Course_UserServiceImpl;
import com.offcn.utlis.BaseServlet;
import com.offcn.utlis.PageUtils;
import com.offcn.utlis.ResultVo;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

@WebServlet("/courseUser")
public class Course_UserServlet extends BaseServlet {
        // 调用工具类
        ResultVo resultVo = null;
        ObjectMapper objectMapper = new ObjectMapper();
        Course_UserService courseUserService = new Course_UserServiceImpl();

        // 分页查询
    public void find(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int currentPage = Integer.parseInt(req.getParameter("currentPage")); // 当前页
        int pageSize = Integer.parseInt(req.getParameter("pageSize")); // 记录
        String search = req.getParameter("search"); // 查询

        PageUtils<Course_User> pageUtils =  courseUserService.findAll(currentPage,pageSize,search);
        resultVo = new ResultVo(200,"查询成功",pageUtils);
        resp.getWriter().println(objectMapper.writeValueAsString(resultVo));
    }

    //修改
    public void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
        int cid = Integer.parseInt(req.getParameter("cid")) ;
        int id = Integer.parseInt(req.getParameter("id")) ;
        boolean b =  courseUserService.updateByid(cid,id);
        if (b){
            resultVo = new ResultVo(200,"修改课程成功",null);
        }else {
            resultVo = new ResultVo(500,"修改课程失败",null);
        }
        resp.getWriter().println(objectMapper.writeValueAsString(resultVo));
    }


    // 删除
    public void del(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String[] ids = req.getParameterValues("id");
           boolean  b =  courseUserService.deleteByid(ids);
           if (b){
               resultVo = new ResultVo(200,"删除成功...",null);
           }else {
               resultVo = new ResultVo(500,"删除失败...",null);
           }
           resp.getWriter().println(objectMapper.writeValueAsString(resultVo));
    }
}
